package com.vitabuybuy.tv.ui.shopcenter

import android.os.Bundle
import android.os.Handler
import android.view.inputmethod.EditorInfo
import androidx.core.os.bundleOf
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivitySearchStoreResultBinding
import com.vitabuybuy.tv.dto.StoreListDto
import com.vitabuybuy.tv.ui.shopcenter.store.StoreHomeActivity
import com.vitabuybuy.tv.ui.shopcenter.viewmodel.SearchGoodsOrStoreViewModel
import com.vitabuybuy.tv.utils.KeyBoardUtils
import kotlinx.android.synthetic.main.activity_cate_search_goods.refresh
import kotlinx.android.synthetic.main.activity_search_store_result.etSearch
import kotlinx.android.synthetic.main.activity_search_store_result.iv_back

class SearchStoreResultActivity :
    BaseActivity<SearchGoodsOrStoreViewModel, ActivitySearchStoreResultBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = false
    override val layoutId: Int
        get() = R.layout.activity_search_store_result
    private var pageNumber = 1

    private var searchStr: String? = null
    override fun onGetBundle(bundle: Bundle?) {
        searchStr = bundle!!.getString("searchStr")
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.viewModel = viewModel
        etSearch.setText(searchStr)
    }

    override fun observeData() {
        viewModel.getStoreList(searchStr!!, pageNumber)
    }

    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getStoreList(searchStr!!, pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getStoreList(searchStr!!, pageNumber)
            }
        })
        iv_back.setOnClickListener { finish() }
        etSearch.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                pageNumber = 1
                viewModel.getStoreList(etSearch.text.toString(), pageNumber)
                Handler().postDelayed({
                    KeyBoardUtils.showSoftInputFromWindow(mContext, etSearch)
                }, 300)
                true
            }
            false
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                refresh.onLoad(msg.arg1)
            }
            2 -> {
                StoreHomeActivity.openActivity(
                    mContext,
                    (msg.obj as StoreListDto).id,
                    (msg.obj as StoreListDto).name
                )
            }
        }
    }

    companion object {
        fun openActivity(
            mContext: BaseActivity<*, *>,
            searchStr: String?
        ) {
            mContext.startActivity(
                bundleOf(
                    Pair("searchStr", searchStr)
                ),
                SearchStoreResultActivity::class.java
            )
        }
    }
}
